//
//  MyGiftViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/11.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MyGiftViewController.h"
#import "ScanViewController.h"
#import "ShakeViewController.h"
#import "MyIdentityTableViewController.h"
#import "LikeTheGiftViewController.h" // 我喜欢的礼物
const CGFloat TopViewH = 250; //图片的高度

@interface MyGiftViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,copy)NSString * result;
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)UILabel *identity;//身份  男孩或女孩 什么职业
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *labell;
@property(nonatomic,strong)UIButton *buttonImage;
@property(nonatomic,strong)UIImageView *myImage;
@property(nonatomic,strong)UIView *myView;
@property(nonatomic,strong)UIView *line;
@property(nonatomic,strong)NSArray *theTitle;

@end

@implementation MyGiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的";
    self.view.backgroundColor = [UIColor purpleColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kP6width(280), kScreenHeight - kP6Height(64)) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    self.tableView.contentInset = UIEdgeInsetsMake(TopViewH * 0.9, 0, 0, 0);
    self.myImage = [[UIImageView alloc]init];
    self.myImage.image = [UIImage imageNamed:@"67536950593448203.jpg"];
    self.myImage.frame = CGRectMake(0, -TopViewH, kScreenWidth, TopViewH);
    self.myImage.userInteractionEnabled = YES;
    self.myImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableView addSubview:_myImage];
    [self.tableView insertSubview:_myImage atIndex:0];
    
    //头像
    self.buttonImage = [UIButton buttonWithType:UIButtonTypeSystem];
    _buttonImage.frame = CGRectMake((kScreenWidth - kP6width(95) - kP6width(70)) / 2, kP6Height(60), kP6width(70), kP6Height(70));
    _buttonImage.backgroundColor = [UIColor whiteColor];
    _buttonImage.layer.borderWidth = 2;
    _buttonImage.layer.masksToBounds = YES;
    _buttonImage.layer.cornerRadius = _buttonImage.bounds.size.height/2;
    [_buttonImage setBackgroundImage:[UIImage imageNamed:@"icon_loading_action2@2x"] forState:UIControlStateNormal];
    [_buttonImage addTarget:self action:@selector(buttonImageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myImage addSubview:_buttonImage];
    
    //名字
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(kP6width(40), kP6Height(140), kP6width(200), kP6Height(30))];
    name.text = @"点击更换头像";
    name.font = [UIFont systemFontOfSize:15];
    name.textColor = [UIColor blackColor];
    
    name.textAlignment = NSTextAlignmentCenter;
    [self.myImage addSubview:name];

}
//点击头像的触发事件(调用系统相册)
-(void)buttonImageAction:(UIButton *)button
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"选择照片", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
}

#pragma mark - ActionSheet的触发方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //调用系统相机
        // 如果有系统相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            
            //摄像头
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }
        // 如果没有系统相机 提示用户
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备没有摄像头" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
        }
    }
    if (buttonIndex == 1)
    {
        //调用系统照片
        UIImagePickerController *pickerImage = [[UIImagePickerController alloc]init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
            pickerImage.delegate = self;
            // 是否可以编辑
            pickerImage.allowsEditing = NO;
            [self presentViewController:pickerImage animated:YES completion:nil];
        }
    }
}

#pragma mark - 拍摄完成后或者选择相册完成后自动调用的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 得到照片 设置头像 button 的背景图片 并且将标题设置为空
    [self.buttonImage setBackgroundImage:[info objectForKey:UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
    [self.buttonImage setTitle:@"" forState:UIControlStateNormal];
    
    //模态返回
    [self dismissViewControllerAnimated:YES completion:nil];
}
//下拉图片放大
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat down = - (TopViewH * 0.9) - scrollView.contentOffset.y;
    if (down < 0)
    {
        return;
    }
    CGRect frame = self.myImage.frame;
    frame.size.height = TopViewH + down; //系数   决定速度
    self.myImage.frame = frame;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
// 复用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *dentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:dentifier];
    }
    //取消点击 cell 时的背景颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.theTitle = [NSArray array];
    self.theTitle= @[@"我的身份",@"心愿单",@"喜欢的攻略",@"摇一摇",@"扫一扫",@"我的附近"];
    for (int i = 0; i < _theTitle.count; i++)
    {
        if (indexPath.row == i)
        {
            cell.imageView.image = [UIImage imageNamed:@"recommend"];
            cell.textLabel.text = _theTitle[i];
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.accessoryType =   UITableViewCellAccessoryDisclosureIndicator;
            if (indexPath.row == 5)
            {
                UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, kP6Height(44), kP6width(280), kP6Height(1))];
                line.backgroundColor = [UIColor blackColor];
                line.alpha = 0.1;
                [cell addSubview:line];
            }
            else
            {
                UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(kP6width(50), kP6Height(44), kP6width(280), kP6Height(1))];
                line.backgroundColor = [UIColor blackColor];
                line.alpha = 0.1;
                [cell addSubview:line];
            }
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        MyIdentityTableViewController *myIdentity = [[MyIdentityTableViewController alloc]init];
        [self.navigationController pushViewController:myIdentity animated:YES];
    }
    else if (indexPath.row == 1)
    {
        LikeTheGiftViewController *likeTheGift = [[LikeTheGiftViewController alloc]init];
        [self.navigationController pushViewController:likeTheGift animated:YES];
    }
    else if (indexPath.row == 4)
    {
        ScanViewController * scanVC = [[ScanViewController alloc]init];
        scanVC.block = ^(NSString *str)
        {
            self.result = [NSString stringWithFormat:@"%@",str];
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:scanVC animated:YES];
    }
    else if(indexPath.row == 3)
    {
        ShakeViewController * shakeVC = [[ShakeViewController alloc]init];
        [self.navigationController pushViewController:shakeVC animated:YES];
    }
}


// 传值 从前往后传值 传的是 我的身份的内容
-(void)viewWillAppear:(BOOL)animated
{
    self.label1 = [[UILabel alloc]init];
    self.label1.text = [[NSUserDefaults standardUserDefaults]valueForKey:@"sex"];
    self.labell = [[UILabel alloc]init];
    self.labell.text = [[NSUserDefaults standardUserDefaults]valueForKey:@"role"];
    self.identity.text = [NSString stringWithFormat:@"%@ %@",_label1.text,_labell.text];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
